Home:ALL Converter>django website is giving 404 error for static files

django website is giving 404 error for static files

Ask Time:2018-01-15T00:47:59         Author:CorpusCallosum

Json Formatter

Having problem with staticfiles in Django 2.

        BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
        STATIC_URL = '/static/'
        STATIC_ROOT = BASE_DIR + '/static/'
        STATICFILES_DIR = [
              (BASE_DIR + "static/"),
        ]


        STATICFILES_FINDERS = [
              'django.contrib.staticfiles.finders.FileSystemFinder',
             'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        ]

And my static structure is like below

enter image description here

I am getting 404 for statics. Tried different things but no success. btw collectstatic has been already performed. Another thing is when i make changes in the static folder, collectstatic is not collecting anything. it says "unmodified".

i am newbie in Django. appreciate for your help.


UPDATE 2:

New structure is below enter image description here

and the settings is like

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static' )
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static_files')
]

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

When i try to collectstatic, there is no change. seems it is not collecting. and 404 returns for statics naturally since it is not collecting.

Author:CorpusCallosum,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48251860/django-website-is-giving-404-error-for-static-files
j-i-l :

There are 2 issues:\n\n1. is the syntax error that Digitiain pointed out in his answer: \n\nSTATICFILES_DIRS = [\n os.path.join(BASE_DIR, \"static\"),\n]\n\n\n2. You cannot add the path in STATIC_ROOT to your STTATICFILES_DIRS. So if your STTATICFILES_DIRS looks like above, STATIC_ROOT cannot be os.path.join(BASE_DIR, \"static\").\n\n\n\nThe first point should be fairly clear. The second also confused me in the beginning. But it makes a lot of sense actually: \n\nIn STATICFILES_DIRS you tell django where to go and look for static files. This not primarily to serve them, but to collect them! The point is that on a productive server, django is not supposed to serve static files; this task should be handled by the web server. Check the docs for details. The idea is that you run the command python manage.py collectstatic and django will go through all the paths in STATICFILES_DIRS, collect the static files and put them... - exactly! - in the STATIC_ROOT folder. Now it becomes clear why the STATIC_ROOT folder cannot really be part of STATICFILES_DIRS as this would mean django would need to put the folder into itself. Again, this is in the case of a productive server. During development django can handle static files without help of a web server, here is more from the docs on that.\n\nI hope that helps to clarify the relation between STATICFILES_DIRS and STATIC_ROOT. Bottom-line: Choose a different path for STATIC_ROOT and you should be good to go!\n\n\n\nAs an example, you could have a project structure like this:\n\nbase_dir/\n - static/\n - css/\n - js/\n - ...\n - static_collected/\n - ...\n\n\nThen it would be valid to define:\n\n# settings.py\nSTATICFILES_DIRS = [\n os.path.join(BASE_DIR, \"static\"),\n]\nSTATIC_ROOT = os.path.join(BASE_DIR, \"static_collected\")\n",
2018-01-14T19:44:25
yy